<template>
  <div class="search-container">
    <div class="search-header">
      <div class="back-img">
        <img src="images/back.png" alt @click="searchback" />
      </div>
      <div class="import">
        <div class="search-icon"></div>
        <input type="text" placeholder="芭比娃娃" v-model="message" />
      </div>
      <div class="btn" @click="searchpro">
        <span>搜索</span>
      </div>
    </div>
    <div class="search-list">
      <div v-for="(item,index) in search" :key="index">{{item}}</div>
    </div>
    <div class="search-body">
      <div class="recently-search" v-if="isSearch">
        <div class="recently-header">
          <p>最近搜索</p>
          <i class="search-sprites" @click="del"></i>
        </div>
        <div class="history-wrap">
          <div v-for="item in searchList" :key="item" class="history">
            <span>{{item}}</span>
          </div>
        </div>
      </div>
      <div class="hot-search">
        <p>热门搜索</p>
        <div @click="tab">{{title}}</div>
      </div>
      <div class="hot-search-tags" v-if="seen">
        <div v-for="item in list" :key="item" class="tag-wrap">
          <span>{{item}}</span>
        </div>
      </div>
      <div class="hidden" v-if="!seen">
        <div>已隐藏搜索发现</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "search",
  data() {
    return {
      seen: true,
      isSearch: false,
      searchshowList: ["纸", "纸巾", "纸张", "纸上谈兵", "纸包鱼", "百度地图"],
      title: "隐藏",
      list: [
        "铝框拉杆箱",
        "不锈钢水壶",
        "空气炸锅",
        "苏打水",
        "地中海窗帘",
        "壁挂音箱",
        "破壁机",
        "计算器",
        "篮球",
        "白板",
        "游戏本",
        "多层保温饭盒"
      ],
      message: "",
      searchList: []
    };
  },
  computed: {
    search() {
      if (this.message) {
        return this.searchshowList.filter(value => {
          return value.includes(this.message);
        });
      }
    }
  },

  methods: {
    searchback() {
      this.$router.go(-1);
    },
    tab() {
      if (this.title == "隐藏") {
        this.title = "显示";
        this.seen = false;
      } else {
        this.title = "隐藏";
        this.seen = true;
      }
    },
    searchpro() {
      this.isSearch = true;
      if (this.message !== "") {
        this.searchList.push(this.message);
        localStorage.setItem("history", JSON.stringify(this.searchList));
        this.message = "";
      }
    },
    del() {
      this.searchList = [];
      this.isSearch = false;
      localStorage.removeItem("history");
    }
  },
  created() {
    let history = localStorage.getItem("history");

    if (history) {
      history = JSON.parse(history);
      this.searchList = history;
      // if (this.searchList.length == 0) {
      //   this.isSearch = false;
      // }
      this.isSearch = true;
    } else {
      this.isSearch = false;
    }
  }
};
</script>

<style scoped>
.search-container {
  width: 100%;
  height: 667px;
  background: rgb(246, 246, 246);
  position: relative;
  top: 0;
  left: 0;
}

.search-header {
  width: 100%;
  height: 44px;
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  background: rgb(255, 255, 255);
  border-bottom: 1px solid rgb(229, 229, 229);
}

.back-img {
  width: 40px;
  height: 44px;
  /* line-height: 44px;
  text-align: center; */
  flex-shrink: 0;
}

.back-img img {
  display: inline-block;
  width: 20px;
  height: 20px;
  margin: 12px 0 0 10px;
}

.btn {
  width: 55px;
  height: 44px;
  line-height: 44px;
  flex-shrink: 0;
}

.btn span {
  margin-left: 5px;
  padding: 5px;
  box-sizing: border-box;
  background-color: #e93b3d;
  color: #fff;
  border-radius: 4px;
  font-size: 14px;
  height: 30px;
}

.import {
  width: 100%;
  border: none;
  border-radius: 15px;
  height: 30px;
  flex-grow: 1;
  background-color: rgb(247, 247, 247);
  display: flex;
  flex-direction: row;
  align-items: center;
}

.search-icon {
  width: 18px;
  height: 15px;
  display: inline-block;
  background: url(https://st.360buyimg.com/so/images/search/jd-sprites.png?__inline)
    no-repeat;
  background-position: -80px 0;
  background-size: 200px;
  margin-left: 10px;
  margin: 0 5px 0 15px;
  z-index: 1;
  flex-shrink: 0;
}

.import input {
  border: 0;
  background: 0 0;
  font-size: 12px;
  color: #232326;
  padding-left: 5px;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  height: auto;
  margin: 0 0 0 5px;
  vertical-align: middle;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
  line-height: 30px;
  outline: none;
}

.search-body {
  width: 100%;
  /* height: 100%; */
  /* background-color: red; */
  position: absolute;
  top: 44px;
  left: 0;
  bottom: 0;
  z-index: 100;
  /* background: #fff; */
}

.hot-search {
  /* margin-top: 15px; */
  padding: 15px 15px 11px 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  background: #fff;
  padding-left: 13px;
}

.hot-search p {
  font-size: 15px;
  line-height: 15px;
  color: #232326;
  width: 100px;
  padding-left: 1px;
}

.hot-search div {
  color: #999;
  font-size: 15px;
  line-height: 15px;
  cursor: pointer;
  user-select: none;
}

.hot-search-tags {
  padding-right: 2px;
  overflow: hidden;
  font-size: 12px;
  text-align: center;
  background: #fff;
  padding-left: 13px;
}

.tag-wrap span {
  display: block;
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: rgb(240, 242, 245);
  padding: 0px 13px;
  line-height: 24px;
}

.tag-wrap {
  padding-right: 10px;
  padding-bottom: 10px;
  float: left;
}

.hidden {
  height: 100px;
  padding-right: 2px;
  overflow: hidden;
  font-size: 12px;
  text-align: center;
  background: #fff;
  padding-left: 13px;
}

.hidden div {
  height: 30px;
  margin: 29px auto 40px;
  line-height: 30px;
  text-align: center;
  color: #999;
}

.recently-search {
  background: #fff;
  width: 100%;
  /* height: 100%; */
  overflow: hidden;
  padding: 15px 15px 11px 13px;
}

.recently-header {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 11px;
}

.recently-search p {
  font-size: 15px;
  line-height: 15px;
  color: #232326;
  /* float: left; */
  width: 100px;
  padding-left: 1px;
}

.search-sprites {
  /* float: right; */
  display: block;
  background: url("../../public/images/search-sprites.png") no-repeat;
  height: 15px;
  width: 15px;
  background-position: 0 -35px;
  background-size: 50px 50px;
  position: relative;
  top: 2px;
}

.history {
  padding-right: 10px;
  padding-bottom: 10px;
  flex-shrink: 0;
}

.history span {
  display: block;
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: rgb(240, 242, 245);
  padding: 0px 13px;
  line-height: 24px;
}

.history-wrap {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  align-items: center;
}

.search-list {
  position: absolute;
  top: 44px;
  left: 0;
  height: auto;
  width: 100%;
  background: #999;
  z-index: 1000;
}
</style>